package com.gu.alipaytestboot.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.gu.alipaytestboot.config.AlipayConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.Gu
 * @date 2020/1/14 17:29
 * @function :  支付宝交易订单查询控制层
 **/
@RestController
@RequestMapping("/query")
public class QueryOrderController {

    /**
     * 查询订单状态
     * @param orderId
     * @return
     */
    @RequestMapping("/queryOrder")
    public Map queryOrder(String orderId) {

        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gatewayUrl,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        Map<String,String> param = new HashMap<>();
        //商户订单号，商户网站订单系统中唯一订单号
        param.put("out_trade_no",orderId);

        alipayRequest.setBizContent(JSON.toJSONString(param));

        //请求
        try {
            String result = alipayClient.execute(alipayRequest).getBody();
            //System.out.println(result);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return  null;
    }

}
